import tkinter as tk
from tkinter import Toplevel, Text, Scrollbar
from tkinter import font
from random import choice
import json

def select_cards(root):
    # 创建一个新的Toplevel窗口
    top = Toplevel(root)
    top.title("抽卡")

    # 设置窗口大小
    top.geometry("1200x1000")

    # 定义全局函数，以便在 create_ui 函数中使用
    def load_cards():
        with open('card.json', 'r', encoding='utf-8') as file:
            cards = json.load(file)['cards']
            global card_fragments
            card_fragments = {card['id']: 0 for card in cards}  # 初始化卡片碎片数量
            for card in cards:
                if 'fragments' in card:
                    card_fragments[card['id']] = card['fragments']
            return cards

    # 更新卡片碎片数量并保存到文件
    def update_card_fragments(card_id, increment=5):
        global card_fragments
        card_fragments[card_id] += increment
        with open('card.json', 'r', encoding='utf-8') as file:
            data = json.load(file)
        for card in data['cards']:
            if card['id'] == card_id:
                card['fragments'] = card_fragments[card_id]
                break
        with open('card.json', 'w', encoding='utf-8') as file:
            json.dump(data, file, ensure_ascii=False, indent=4)

    # 定义加载目标的函数
    def load_goal():
        with open('goal.json', 'r', encoding='utf-8') as file:
            goal_data = json.load(file)
        return goal_data['goal']

    # 更新目标值并保存到文件
    def update_goal(decrement=1):
        with open('goal.json', 'r', encoding='utf-8') as file:
            data = json.load(file)
        goal = data['goal'] - decrement
        if goal < 0:  # 确保目标值不会变成负数
            goal = 0
        data['goal'] = goal
        with open('goal.json', 'w', encoding='utf-8') as file:
            json.dump(data, file, ensure_ascii=False, indent=4)
        return goal

    # 定义show_card函数
    def show_card():
        global card_name_label, card_description_label, goal_label, start_button
        card = choice(load_cards())

        # 从文件加载当前的goal值
        with open('goal.json', 'r', encoding='utf-8') as file:
            goal_data = json.load(file)
            current_goal = goal_data['goal']

        # 检查goal值是否足够抽卡
        if current_goal > 0:
            # 更新卡片碎片数量并保存到文件
            update_card_fragments(card['id'])

            # 更新目标值并更新显示框中的内容
            new_goal = update_goal(1)
            goal_label.config(text=f"剩余抽卡次数：{new_goal}")

            # 设置名称和描述的字体样式
            name_font = font.Font(family="Helvetica", size=20, weight="bold")
            description_font = font.Font(family="Helvetica", size=18)

            # 更新名称和描述标签
            card_name_label.config(text=card['name'], font=name_font)
            card_description_label.config(text=card['description'], font=description_font)
        else:
            # 如果goal值小于或等于0，禁用开始按钮并显示提示信息
            start_button.config(state="disabled")
            goal_label.config(text="当前抽卡次数不足")

    # 创建UI
    def create_ui(top):
        global card_name_label, task_button, card_description_label, start_button, goal_label

        # 设置窗口背景颜色
        top.configure(bg='white')

        # 设置按钮的字体大小
        button_font = tk.font.Font(size=20)

        # 创建显示目标的标签
        goal_label = tk.Label(top, bg='white', fg='black')
        goal_label.pack(side=tk.TOP, pady=5)

        # 更新显示目标的标签内容
        goal = load_goal()
        goal_label.config(text=f"剩余抽卡次数：{goal}")

        # 创建开始抽卡的按钮
        start_button = tk.Button(top, text="开始抽卡", command=show_card, bg='white', font=button_font)
        start_button.pack(side=tk.TOP, pady=5)

        # 创建Canvas用于绘制黑色方框，并放置其他控件
        canvas = tk.Canvas(top, bg='white', width=200, height=150)
        canvas.pack(side=tk.TOP, pady=80, anchor='center')

        # 在Canvas上绘制黑色方框
        canvas.create_rectangle(0, 0, 203, 147, outline='black', width=5)

        # 创建显示卡片信息的容器
        card_info_container = tk.Frame(canvas, bg='white', width=200, height=150)
        canvas.create_window(100, 75, anchor='center', window=card_info_container)  # 将Frame放置在Canvas上

        # 创建显示卡牌名称的标签
        card_name_label = tk.Label(card_info_container, bg='white', fg='black')
        card_name_label.pack(side=tk.TOP, pady=10)

        # 创建显示卡牌描述的标签
        card_description_label = tk.Label(card_info_container, bg='white', fg='black')
        card_description_label.pack(side=tk.TOP, pady=5)

    # 创建UI
    create_ui(top)